define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fastim/user/index' + location.search,
                    add_url: 'fastim/user/add',
                    edit_url: 'fastim/user/edit',
                    del_url: 'fastim/user/del',
                    multi_url: 'fastim/user/multi',
                    import_url: 'fastim/user/import',
                    table: 'fastim_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {
                        //     field: 'type',
                        //     title: __('Type'),
                        //     searchList: {"user": __('Type user'), "csr": __('Type csr'), "tourist": __('Type tourist')},
                        //     formatter: Table.api.formatter.normal
                        // },
                        // {field: 'usergroup.name', title: __('会员组'), operate: 'LIKE'},
                        // {field: 'csrgroup.name', title: __('Group_id'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'admin_id', title: __('Admin_id')},
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            operate: 'LIKE',
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        // {field: 'referrer', title: __('Referrer'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'fauser.mobile', title: __('FaUser.Mobile'), operate: 'LIKE', visible: false},
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        // {field: 'gender', title: __('Gender'), searchList: {"secrecy":__('Gender secrecy'),"male":__('Gender male'),"female":__('Gender female')}, formatter: Table.api.formatter.normal},
                        // {field: 'age', title: __('Age')},
                        // {field: 'birthday', title: __('Birthday'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'bio', title: __('Bio'), operate: 'LIKE'},
                        // {field: 'occupation', title: __('Occupation')},
                        // {field: 'company', title: __('Company'), operate: 'LIKE'},
                        // {field: 'fauser.nickname', title: __('FaUser.nickname'), operate: 'LIKE'},
                        // {field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
                        {field: 'ip', title: __('限制ip'), operate: 'LIKE'},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {
                                "0": __('Status 0'),
                                "1": __('Status 1'),
                                "2": __('Status 2'),
                                "3": __('Status 3')
                            },
                            formatter: Table.api.formatter.status
                        },
                        // {field: 'note', title: __('Note'), operate: 'LIKE'},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'black',
                                    title: __('解封'),
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: 'fa fa-lock',
                                    url: 'fastim/user/black',
                                    refresh: true,
                                    visible: function(row) {
                                        return row.isblack
                                    }
                                },
                                {
                                    name: 'black',
                                    title: __('封禁'),
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: 'fa fa-unlock',
                                    url: 'fastim/user/black',
                                    refresh: true,
                                    visible: function(row) {
                                        return !row.isblack
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'fastim/user/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'fauser.mobile', title: __('FaUser.Mobile'), operate: 'LIKE', visible: false},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'fastim/user/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'fastim/user/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $(document).on("change", "#c-type", function (e) {
                    if ($(e.target).val() == 'user') {
                        $(".user-group").removeClass("hidden");
                    } else {
                        $(".user-group").addClass("hidden");
                    }
                    if ($(e.target).val() == 'csr') {
                        $(".csr-group").removeClass("hidden");
                    } else {
                        $(".csr-group").addClass("hidden");
                    }
                });

                $("#c-type").trigger("change");
            }
        }
    };
    return Controller;
});